//
//  NSString+FLL_MD5.m
//  24-加密
//
//  Created by LL.F on 16/8/16.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "NSString+FLL_MD5.h"
// 要用加密就要引用这个系统的库
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (FLL_MD5)
/******************  32位   *******************/
- (NSString *)stringWith32BitMD5
{
    
    // <1> 将字符串转换成C语言字符串
    const char *cString = [self UTF8String];
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    // <2> 将C的字符串转换成 MD5
    CC_MD5(cString, length, bytes);
    // <3> 将C的字符串转换成OC的字符串
    NSMutableString *finalMD5String = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [finalMD5String appendFormat:@"%02x", bytes[i]];
    }
    return finalMD5String;
    
}
- (NSString *)stringWith32BitMD5Lower
{
    NSString *string = [self stringWith32BitMD5];
    return [string lowercaseString];

}
- (NSString *)stringWith32BitMD5Upper
{
    NSString *string = [self stringWith32BitMD5];
    return [string uppercaseString];
}

/******************  16位   *******************/
- (NSString *)stringWith16BitMD5
{

    NSString *MD5String32bytes = [self stringWith32BitMD5];
    NSString *MD5String16bytes = [MD5String32bytes substringWithRange:NSMakeRange(8, 16)];
    return MD5String16bytes;
    
}
- (NSString *)stringWith16BitMD5Lower
{

    return [[self stringWith16BitMD5] lowercaseString];
}
- (NSString *)stringWith16BitMD5Upper
{

    return  [[self stringWith16BitMD5] uppercaseString];
}

@end
